package com.ssyc.student.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StTreasuerFoodAdapter;
import com.ssyc.student.bean.StTreasuerFood;
import com.ssyc.student.bean.TreasureChestInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 学生端--喂食好友的宠物
 */

public class StudentFeedingActivity extends BaseActivity implements View.OnClickListener {

    //下拉刷新控件
    private SmartRefreshLayout srf;
    //列表控件
    private RecyclerView rv;
    //列表的数据集合
    private List<StTreasuerFood.DataBean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private StTreasuerFoodAdapter adapter;
    //返回
    private ImageView rl_close;

    public UseFoodHandler mHandler;

    //拜访对象账号
    private String userid;

    //拜访对象姓名
    private String toname;

    //拜访宠物姓名
    private String topetname;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_feed_friends;
    }

    @Override
    protected void init() {
        //初始化视图
        initView();

        //接受数据
        initIntent();

        //请求服务器
        http();
    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        srf = findViewById(R.id.srf);
        rv = findViewById(R.id.rv);

        rl_close = findViewById(R.id.rl_close);

        rl_close.setOnClickListener(this);

        mHandler = new UseFoodHandler();
    }

    /**
     * 接收数据
     */
    private void initIntent() {

        userid = getIntent().getStringExtra("userid");
        toname = getIntent().getStringExtra("toname");
        topetname = getIntent().getStringExtra("topetname");


    }

    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_close) {
            //关闭页面
            finish();
        }
    }

    /**
     * 设置下拉刷新控件
     */
    private void initSrl() {
        SrfManager.setSmartByPullListener(srf, null);
        srf.setPrimaryColors(getResources().getColor(R.color.transparent));
    }


    /**
     * 设置列表
     */
    private void initRv() {
        emptyView = View.inflate(this, R.layout.st_rv_treasure_food_empty, null);
        adapter = new StTreasuerFoodAdapter(this,this,
                R.layout.student_rv_common_treasure_food, oldDatas,mHandler);
        //设置横向的布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        BqaManager.setRv(emptyView, gridLayoutManager, this, adapter, rv, null);
    }




    /**
     * 获取百宝箱食物接口数据的网络请求的方法
     */
    private void http() {

        String acc = SPUtil.getString(this, SpKeys.ACCOUNT);
        String token = SPUtil.getString(this,SpKeys.TOKEN1);

        Map<String, String> map = new HashMap<>();
        map.put("type", "4");
        map.put("platform", "8");
        map.put("acc", acc);
        map.put("role", "0");
        map.put("apptoken", token);
        map.put("allboxitem", "0");
        HttpUtils.post(HttpAdress.FEED, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError (Call call, Exception e, int id) {
                showError();
                UiUtils.Toast(BaseApplication.ERROR, false);

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    //解析返回服务器返回的JSON数据
                    showContent();
                    StTreasuerFood info = GsonUtil.jsonToBean(response, StTreasuerFood.class);

                    Log.i("test", "百宝箱食物："+response);
                    if ("200".equals(info.getState())) {
                        if (null != info) {

                            oldDatas = info.getData();
                            //首次加载
                            //设置rv
                            initRv();

                            //设置下拉控件
                            initSrl();

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "解析服务器返回数据异常");
                }
            }
        });
    }



    /**
     * Handler回调使用物品
     */
    public class UseFoodHandler extends Handler {
        public Bundle bundle = new Bundle();

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            //接受Json数据
            bundle = msg.getData();
            String json = bundle.getString("json");
            String Energy = bundle.getString("Energy");

            String acc = SPUtil.getString(StudentFeedingActivity.this, SpKeys.ACCOUNT);
            String token = SPUtil.getString(StudentFeedingActivity.this,SpKeys.TOKEN1);
            String name = SPUtil.getString(StudentFeedingActivity.this,SpKeys.NAME);
            String petname = SPUtil.getString(StudentFeedingActivity.this,SpKeys.PETNAME);


            Map<String, String> map = new HashMap<>();
            map.put("type", "5");
            map.put("platform", "8");
            map.put("acc", acc);
            map.put("role", "0");
            map.put("apptoken", token);
            map.put("toacc", userid);
            map.put("torole", "0");
            map.put("fromname",name );
            map.put("frompetname", petname);
            map.put("toname", toname);
            map.put("topetname", topetname);
            map.put("jsitem", json);

            HttpUtils.post(HttpAdress.FEED, map, this, new HttpUtils.CallBack() {
                @Override
                public void onError(Call call, Exception e, int id) {

                    showError();
                    UiUtils.Toast(BaseApplication.ERROR, false);
                }

                @Override
                public void onResponse(String response, int id) {
                    try {
                        //解析返回服务器返回的JSON数据
                        showContent();
                        TreasureChestInfo info = GsonUtil.jsonToBean(response, TreasureChestInfo.class);

                        Log.i("test", "使用成功" + response);
                        if ("200".equals(info.getState())) {
                            http();
                            UiUtils.Toast("好友宠物增加"+ Energy+"点精力值", false);
                        }else{
                            UiUtils.Toast("使用失败，错误码："+info.getState(), false);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "解析服务器返回数据异常");

                    }
                }
            });
        }
    }
}
